package cn.online.taxi.sso.service.impl;

import cn.online.taxi.common.exception.OnlineTaxiException;
import cn.online.taxi.manager.mapper.TbDriverMapper;
import cn.online.taxi.manager.mapper.TbPassengerMapper;
import cn.online.taxi.manager.pojo.TbDriver;
import cn.online.taxi.manager.pojo.TbDriverExample;
import cn.online.taxi.manager.pojo.TbPassenger;
import cn.online.taxi.manager.pojo.TbPassengerExample;
import cn.online.taxi.sso.service.RegisterService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author BinBini
 * @Date 2021/3/18 16:28
 * @Version 1.0
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private TbPassengerMapper tbPassengerMapper;
    @Resource
    private TbDriverMapper tbDriverMapper;

    @Override
    public boolean checkData(String param, int type) {
        boolean result = true;
        if (type == 0) {
            TbPassengerExample example = new TbPassengerExample();
            TbPassengerExample.Criteria criteria = example.createCriteria();
            criteria.andStateEqualTo(1);
            criteria.andPhoneEqualTo(param);
            List<TbPassenger> list = tbPassengerMapper.selectByExample(example);
            if (list != null && list.size() > 0) {
                result = false;
            }
        }else if (type == 1) {
            TbDriverExample example = new TbDriverExample();
            TbDriverExample.Criteria criteria = example.createCriteria();
            criteria.andPhoneEqualTo(param);
            List<TbDriver> list = tbDriverMapper.selectByExample(example);
            if (list != null && list.size() > 0) {
                result = false;
            }
        }
        return result;
    }

    @Override
    public int passengerRegister(String userName, String phone, String userPwd) {
        TbPassenger tbPassenger = new TbPassenger();
        tbPassenger.setUsername(userName);
        tbPassenger.setPhone(phone);
        if(userName.isEmpty() || userPwd.isEmpty() || phone.isEmpty()) {
            return -1;  // 用户名 密码 手机号不能为空
        }
        boolean result = checkData(phone, 0);
        if(!result) {
            return 0;   // 手机号已被注册
        }
        // MD5 加密
        String md5Pass = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        tbPassenger.setPassword(md5Pass);
        tbPassenger.setBalance(new BigDecimal("0"));
        tbPassenger.setState(1);
        tbPassenger.setCreated(new Date());
        tbPassenger.setUpdated(new Date());

        if(tbPassengerMapper.insert(tbPassenger) != 1) {
            throw new OnlineTaxiException("注册用户失败");
        }
        return 1;
    }

    @Override
    public int driverRegister(String driverName, String phone, String userPwd, Integer sex) {
        TbDriver tbDriver = new TbDriver();
        tbDriver.setDriverName(driverName);
        tbDriver.setPhone(phone);
        tbDriver.setSex(sex);
        if(driverName.isEmpty() || userPwd.isEmpty() || phone.isEmpty() || sex == null) {
            return -1;  // 姓名 密码 手机号不能为空
        }
        boolean result = checkData(phone, 1);
        if(!result) {
            return 0;   // 手机号已被注册
        }
        // MD5 加密
        String md5Pass = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        tbDriver.setPassword(md5Pass);
        tbDriver.setBalance(new BigDecimal("0"));
        tbDriver.setIsFollowing(0);
        tbDriver.setWorkStatus(0);
        tbDriver.setHeadImg("");
        tbDriver.setCityCode("-1");
        tbDriver.setUseStatus(0);
        tbDriver.setCsWorkStatus(0);
        tbDriver.setSignStatus(1);
        tbDriver.setCreated(new Date());
        tbDriver.setUpdated(new Date());

        if(tbDriverMapper.insert(tbDriver) != 1) {
            throw new OnlineTaxiException("注册司机失败");
        }
        return 1;
    }
}
